Use case: Communication with buffers monitoring (polling)

The changes between HAL1 and HAL2 are limited to naming; the overall programming model remains unchanged. For transmission, add the message to a specific TX buffer and enable the transmission request for that buffer. The transmission completion status is obtained by monitoring the TX buffer state. For reception, monitor the RX buffer for incoming messages, and once a message is available, read it from the RX buffer.

Sequence diagram

  @startuml

  participant "User application" as App #AliceBlue
  participant "<b>HAL FDCAN" as FDCAN #19DD3A

  == Dedicated Tx Buffer Transmission (Monitoring) ==

  note over App
  <b>Step 1: Fill a dedicated Tx buffer</b>
  The application prepares the Tx header and data, then fills the selected Tx buffer.
  end note
  App -> FDCAN: HAL_FDCAN_AddMessageToTxBuffer()
  FDCAN --> App: HAL_OK

  note over App
  <b>Step 2: Request transmission from the buffer</b>
  The application requests transmission from the filled Tx buffer.
  end note
  App -> FDCAN: HAL_FDCAN_EnableTxBufferRequest()
  FDCAN --> App: HAL_OK

  note over App
  <b>Step 3: Poll for transmission completion</b>
  The application polls the buffer status until transmission is complete.
  end note
  loop #lightblue until Tx complete
      App -> FDCAN: HAL_FDCAN_IsTxBufferMessagePending()
      alt Tx pending
          FDCAN --> App: <color green>1
      else Tx not pending
          FDCAN --> App: <color orange>0
      end
  end

  == Dedicated Rx Buffer Monitoring ==

  note over App
  <b>Step 4: Poll for new message in a dedicated Rx buffer</b>
  The application checks if a new message has been received in the specified Rx buffer.
  end note
  App -> FDCAN: HAL_FDCAN_IsRxBufferMessageAvailable()
  alt #LightSteelBlue New message received
      FDCAN --> App: <color green>1
      note right
      The flag is cleared by the API.
      The application can now process the received message.
      end note
      App -> FDCAN: HAL_FDCAN_GetRxMessage()
      FDCAN --> App: <color green>HAL_OK
  else #LightCyan No new message
      FDCAN --> App: <color red>0
      note right
      No new message is present in the buffer.
      end note
  end


  @enduml

Code snippet

HAL1

HAL2

/* Prepare FDCAN transmit message header */
TxHeader.Identifier          = 0x111;
TxHeader.IdType              = FDCAN_STANDARD_ID;
TxHeader.TxFrameType         = FDCAN_DATA_FRAME;
TxHeader.DataLength          = FDCAN_DLC_BYTES_8;
TxHeader.ErrorStateIndicator = FDCAN_ESI_ACTIVE;
TxHeader.BitRateSwitch       = FDCAN_BRS_ON;
TxHeader.FDFormat            = FDCAN_FD_CAN;
TxHeader.TxEventFifoControl  = FDCAN_NO_TX_EVENTS;
TxHeader.MessageMarker       = 0;

HAL_FDCAN_AddMessageToTxBuffer(&hfdcan, &TxHeader, TxData, FDCAN_TX_BUFFER0);

HAL_FDCAN_EnableTxBufferRequest(&hfdcan, FDCAN_TX_BUFFER0);

while(HAL_FDCAN_IsTxBufferMessagePending(&hfdcan, FDCAN_TX_BUFFER0) < 1);

while(HAL_FDCAN_IsRxBufferMessageAvailable(&hfdcan, FDCAN_RX_BUFFER1) < 1);

HAL_FDCAN_GetRxMessage(&hfdcan, FDCAN_RX_BUFFER1, &RxHeader, RxData);
/* Prepare FDCAN transmit message header */
tx_element_header.b.identifier            = 0x111,
tx_element_header.b.identifier_type       = HAL_FDCAN_ID_STANDARD,
tx_element_header.b.frame_type            = HAL_FDCAN_FRAME_DATA,
tx_element_header.b.data_length           = HAL_FDCAN_DATA_LEN_CAN_FDCAN_8_BYTE,
tx_element_header.b.error_state_indicator = HAL_FDCAN_ERROR_STATE_IND_ACTIVE,
tx_element_header.b.bit_rate_switch       = HAL_FDCAN_BIT_RATE_SWITCH_ON,
tx_element_header.b.frame_format          = HAL_FDCAN_HEADER_FRAME_FORMAT_FD_CAN,
tx_element_header.b.event_fifo_control    = HAL_FDCAN_FIFO_STORE_TX_EVENTS,
tx_element_header.b.message_marker        = 0U,

HAL_FDCAN_FillMessageToBuffer(&hfdcan, &TxHeader, TxData, HAL_FDCAN_TX_BUFFER_0);

HAL_FDCAN_ReqTransmitMsgFromBuffer(&hfdcan, HAL_FDCAN_TX_BUFFER_0);

while(HAL_FDCAN_IsTxBufferMessagePending(&hfdcan, HAL_FDCAN_TX_BUFFER_0) != HAL_FDCAN_TX_BUFFER_NOT_PENDING);

while(HAL_FDCAN_GetRxBufferMessageStatusAndClearFlag(&hfdcan, HAL_FDCAN_RX_BUFFER_1) != HAL_FDCAN_RX_BUFFER_NEW_MSG_RECEIVED);

HAL_FDCAN_GetReceivedMessage(&hfdcan, FDCAN_RX_BUFFER1, &RxHeader, RxData);

Key changes

  1. In HAL2, the transmit request is performed using HAL_FDCAN_ReqTransmitMsgFromBuffer().

  2. In HAL2, the receive buffer monitoring uses HAL_FDCAN_GetRxBufferMessageStatusAndClearFlag().